from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.user_addr_fields_def import user_addr_fields,user_addr_list_fields
from services.online_mall.user_addr_service import UserAddrService

class UserAddrApi(Resource):
    @login_required
    @marshal_with(user_addr_list_fields)
    @check_perms(perms=["mall:user_addr:list"])
    def get(self):
        parser= reqparse.RequestParser()
        parser.add_argument("account_id",type=str,required=False,location='args')
        parser.add_argument("receiver",type=str,required=False,location='args')
        parser.add_argument("postal_code",type=str,required=False,location='args')
        parser.add_argument("addr",type=str,required=False,location='args')
        parser.add_argument("mobile",type=str,required=False,location='args')
        parser.add_argument("status",type=StrBoolNone,required=False,location='args')
        parser.add_argument("is_default",type=StrBoolNone,required=False,location='args')
        parser.add_argument("page",type=int,required=False,default=-1,location='args')
        parser.add_argument("page_size",type=int,required=False,default=-1,location='args')
        args = parser.parse_args()
        return UserAddrService.get_list(current_user,args)
    
    @login_required
    @marshal_with(user_addr_fields)
    @check_perms(perms=["mall:user_addr:create"])
    def post(self):
        parser= reqparse.RequestParser()
        parser.add_argument("account_id",type=str,required=False,location='json')
        parser.add_argument("receiver",type=str,required=False,location='json')
        parser.add_argument("province_id",type=str,required=False,location='json')
        parser.add_argument("city_id",type=str,required=False,location='json')
        parser.add_argument("district_id",type=str,required=False,location='json')
        parser.add_argument("postal_code",type=str,required=False,location='json')
        parser.add_argument("addr",type=str,required=False,location='json')
        parser.add_argument("mobile",type=str,required=False,location='json')
        parser.add_argument("status",type=StrBoolNone,required=False,location='json')
        parser.add_argument("is_default",type=StrBoolNone,required=False,location='json')
        args = parser.parse_args()
        return UserAddrService.create_user_addr(current_user,args)
    
    @login_required
    @marshal_with(user_addr_fields)
    @check_perms(perms=["mall:user_addr:update"])
    def put(self):
        parser= reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        parser.add_argument("account_id",type=str,required=False,location='json')
        parser.add_argument("receiver",type=str,required=False,location='json')
        parser.add_argument("province_id",type=str,required=False,location='json')
        parser.add_argument("city_id",type=str,required=False,location='json')
        parser.add_argument("district_id",type=str,required=False,location='json')
        parser.add_argument("postal_code",type=str,required=False,location='json')
        parser.add_argument("addr",type=str,required=False,location='json')
        parser.add_argument("mobile",type=str,required=False,location='json')
        parser.add_argument("status",type=StrBoolNone,required=False,location='json')
        parser.add_argument("is_default",type=StrBoolNone,required=False,location='json')
        args = parser.parse_args()
        return UserAddrService.update_user_addr(current_user,args)
    
    @login_required
    @check_perms(perms=["mall:user_addr:del"])
    def delete(self):
        parser= reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        args = parser.parse_args()
        UserAddrService.del_user_addr(current_user,args)
        return {"code":200,"message":"OK"}

class UserAddrInfoApi(Resource):
    @login_required
    @marshal_with(user_addr_fields)
    @check_perms(perms=["mall:user_addr:info"])
    def get(self,addr_id:str):
        return UserAddrService.get_by_id(current_user,addr_id)
    
api.add_resource(UserAddrInfoApi, '/users/addrs/<string:addr_id>')
api.add_resource(UserAddrApi, '/users/addrs')